import re, pickle, pickletools
from gzip import open as gzopen 

class PackageDef:
	def __init__(self):
		self.fields={}
		self.lastfield=''
		self.lastfieldname=None
		self.packagename=None
	
	def add_field(self, fn):
		assert self.packagename == None
		assert not (fn in self.fields)
		#~ if fn in self.fields:
			#~ print 700,fn,self.fields.keys()
			#~ assert 0
		#~ #assert fn != self.lastfieldname
		#~ if fn == self.lastfieldname:
			#~ print 800,fn,self.lastfieldname
			#~ assert 0
		#
		if self.lastfieldname != None:
			self.fields[self.lastfieldname]=self.lastfield
			self.lastfield=''
		#
		#fields[fn]=''
		self.lastfieldname=fn
		
	def append_data(self, fd):
		assert self.packagename == None
		self.lastfield += fd

	def save(self):
		self.packagename = self.fields['Package']

def main(rch):
	curpkg=PackageDef()
	for each in rch.readlines():
#		print 900, len(each), each
		m = re.match("""^(?P<fn>[^:\s]*):\s+(?P<fv>.*)$""",each)
		if m:
			fn=''
			try:
				fn=m.group("fn")
				curpkg.add_field(fn)
			except Exception,e:
				print 480,e,curpkg.fields.keys(),fn,m.groups()
			curpkg.append_data(m.group("fv"))
			continue
		m = re.match("""\s\.""",each)
		if m:
			curpkg.append_data("\n")
			continue
		m = re.match("^$",each)
		if m:
			curpkg.save()
			havpkg(curpkg)
			curpkg = PackageDef()
			continue
		m = re.match("""\s(.*)""",each)
		if m:
			curpkg.append_data(m.group(1))
			continue
		print 99,m,each
		assert 0
	#~ print pickletools.dis(pickle.dumps(all))
	#~ print pickle.dump(all,gzopen('pkg-pickle.gz','w'))
	return all

all={}
n=0

def havpkg(curpkg):
	global all,n
	n+=1
	if n>100: assert 'finifshed'==0
	all[curpkg.packagename]=curpkg
	#
	gen1(curpkg.packagename,curpkg)

def gen(xall):
	for pn,po in xall.iteritems():
		gen1(pn,po)
def gen1(pn,po):
	f=open('a/%s.html'%pn,'w')
	f.write('<html><body><table>')
	for x,y in po.fields.iteritems():
		f.write('<tr><td><b>%s</b></td><td> %s</td></tr>'%(x,y))
	f.write('</table></body></html>')
	f.close()

all=main(open("Packages"))
#gen(all)
